package com.lzxuni.modules.Seed.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.R;
import com.lzxuni.modules.Seed.entity.SeedLimit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.Seed.service.SeedLimitService;
import org.springframework.web.bind.annotation.RestController;
import com.lzxuni.modules.common.controller.BaseController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 天气信息_苗青管理_临界值 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/Seed/Limit")
public class SeedLimitController extends BaseController {

    @Autowired
    private SeedLimitService seedLimitService;
    private String templatesPath="/Seed/Limit";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView(templatesPath+"/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,SeedLimit seedLimit){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        PageData pageData = getPageData(seedLimitService.queryPage(pageParameter, seedLimit));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form() {
        ModelAndView mv = new ModelAndView(templatesPath+"/form");
        return mv;
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        seedLimitService.removeById(keyValue);
        return R.ok("删除成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,SeedLimit seedLimit) {
        seedLimit.setLimitId(keyValue);
        seedLimit.setCropId(seedLimit.getCropModel());
        if(StringUtils.isEmpty(seedLimit.getLimitId())){
             seedLimitService.save(seedLimit);
             return R.ok("保存成功");
        }else{
            seedLimitService.updateById(seedLimit);
            return R.ok("修改成功");
        }
    }


    //查询解决方案
    @RequestMapping("/GetLimitScheme")
    public Object getLimitScheme(SeedLimit seedLimit){
        if(seedLimit!=null&&StringUtils.isNotBlank(seedLimit.getCropId())&&StringUtils.isNotBlank(seedLimit.getSeedName())){
            SeedLimit seedLimit1=seedLimitService.getOne(new QueryWrapper<SeedLimit>(seedLimit));
            if(seedLimit1!=null&&StringUtils.isNotBlank(seedLimit1.getLimitScheme())){
                return R.ok().put("data", seedLimit1.getLimitScheme());
            }
        }
        return R.ok().put("data", "");
    }


}

